using System;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gta.Common;

namespace Yoozoo.Gameplay.City
{
    public class HangingLevelTrigger : MonoBehaviour
    {
        public int levelId;
        public int eventType; // 0:条件未达成的事件 1: 普通事件 2: Boss事件 

        public GameObject commonEventEffect;
        public GameObject commonEventGuideEffect;

        public GameObject bossEventEffect;
        public GameObject bossEventGuideEffect;

        public GameObject greyEventEffect;
        public GameObject greyEventGuideEffect;
        
        private Collider enterPlayerCollier;

        private void Awake()
        {
        }

        private void Update()
        {
            if (enterPlayerCollier == null)
            {
                // 出圈
                updateEffect(false);
            }
            else
            {
                // 进圈
                updateEffect(true);
            }
        }
        
        void updateEffect(bool isTrigger)
        {
            switch (eventType)
            {
                case 0:
                    {
                        // 条件未达成的事件
                        deactiveGameObject(commonEventEffect);
                        deactiveGameObject(commonEventGuideEffect);
                        deactiveGameObject(bossEventEffect);
                        deactiveGameObject(bossEventGuideEffect);
                        activeGameObject(greyEventEffect);
                        if (isTrigger)
                        {
                            deactiveGameObject(greyEventGuideEffect);
                        }
                        else
                        {
                            activeGameObject(greyEventGuideEffect);
                        }
                    }
                    break;
                case 1:
                    {
                        // 普通事件
                        activeGameObject(commonEventEffect);
                        if (isTrigger)
                        {
                            deactiveGameObject(commonEventGuideEffect);
                        }
                        else
                        {
                            activeGameObject(commonEventGuideEffect);
                        }
                        deactiveGameObject(bossEventEffect);
                        deactiveGameObject(bossEventGuideEffect);
                        deactiveGameObject(greyEventEffect);
                        deactiveGameObject(greyEventGuideEffect);
                    }
                    break;
                case 2:
                    {
                        // Boss事件
                        deactiveGameObject(commonEventEffect);
                        deactiveGameObject(commonEventGuideEffect);
                        activeGameObject(bossEventEffect);
                        if (isTrigger)
                        {
                            deactiveGameObject(bossEventGuideEffect);
                        }
                        else
                        {
                            activeGameObject(bossEventGuideEffect);
                        }
                        deactiveGameObject(greyEventEffect);
                        deactiveGameObject(greyEventGuideEffect);
                    }
                    break;
            }
        }

        
        void activeGameObject(GameObject go)
        {
            if (!go.activeSelf)
            {
                go.SetActive(true);
            }
        }
        void deactiveGameObject(GameObject go)
        {
            if (go.activeSelf)
            {
                go.SetActive(false);
            }
        }

        private void OnTriggerEnter(Collider other)
        {
            if (LibertyAIUtils.IsPlayerTriggerCollider(other))
            {
                enterPlayerCollier = other;
                ResidentHandler.Inst.GetFunction("CityLuaFunction.onPlayerHangingTriggerEnter").Action(levelId);
            }
        }

        private void OnTriggerStay(Collider other)
        {
            if (enterPlayerCollier == null)
            {
                if (LibertyAIUtils.IsPlayerTriggerCollider(other))
                {
                    enterPlayerCollier = other;
                    ResidentHandler.Inst.GetFunction("CityLuaFunction.onPlayerHangingTriggerEnter").Action(levelId);
                }
            }
        }

        private void OnTriggerExit(Collider other)
        {
            if (LibertyAIUtils.IsPlayerTriggerCollider(other))
            {
                enterPlayerCollier = null;
                ResidentHandler.Inst.GetFunction("CityLuaFunction.onPlayerHangingTriggerExit").Action(levelId);
            }
        }
    }
}